package com.shire.shire_interface.controller.validation;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.model.validation.WxValidationRecord;
import com.shire.shire_interface.model.validation.WxValidationRecordExample;
import com.shire.shire_interface.service.account.WxAccountService;
import com.shire.shire_interface.service.commandcenter.BaseCommandProcessor;
import com.shire.shire_interface.service.commandcenter.WxCommandService;
import com.shire.shire_interface.service.validation.WxValidationRecordService;
import com.shire.shire_interface.service.validation.WxValidationRuleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Api(tags = "加好友 好友验证")
@RestController
@RequestMapping("/validation")
public class WxValidationRecordController extends BaseController {

    @Resource
    private WxValidationRecordService wxValidationRecordService;
    @Resource
    private WxAccountService accountService;
    @Resource
    private WxCommandService commandService;
    @Resource
    private BaseCommandProcessor commandProcessor;
    @Resource
    private WxValidationRuleService wxValidationRuleService;

//    @ApiOperation(value = "好友验证列表 csId, wxAccount")
    @ResponseBody
    @PostMapping("/list")
    public Response list(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            Integer status = jsonObject.getInteger("status");

            WxValidationRecordExample example = new WxValidationRecordExample();
            example.createCriteria().andWxaccountEqualTo(wxAccount);
            example.setOrderByClause("update_time desc");
            List<WxValidationRecord> wxValidationRecords = wxValidationRecordService.selectByExample(example);
            response.setData(wxValidationRecords);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "同步帐号验证信息 wxAccount")
    @ResponseBody
    @PostMapping("/sync")
    public Response syncValidationRecords(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");
            WxAccount account = accountService.getWxAccountByWxId(wxAccount);
            wxValidationRuleService.syncValidationList(getCurrentUser(session).getId(),account.getId());
            response.success("成功");
        } catch (Exception e) {
            response.error("失败:"+e.getMessage());
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "通过验证 csId, wxAccount, friendAccountList")
    @ResponseBody
    @PostMapping("/insert")
    public Response insertValidationRule(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");

            WxAccount account = accountService.getWxAccountByWxId(wxAccount);

            List<String> friendAccountList = (List<String>) jsonObject.get("friendAccountList");
            WxValidationRecordExample example = new WxValidationRecordExample();
            example.createCriteria().andWxaccountEqualTo(wxAccount).andWxidIn(friendAccountList);

            List<WxValidationRecord> recordList = wxValidationRecordService.selectByExample(example);
            for (WxValidationRecord record : recordList) {
                JSONObject firstMsg = (JSONObject) JSONObject.parseArray(record.getMsglist()).get(0);

                JSONObject reqObject = new JSONObject();
                reqObject.put("ticket", firstMsg.getString("ticket"));

                WxCommand command = commandService.generateCmdObject(getCurrentUser(session).getId(),account.getId(),reqObject.toJSONString(),WxConstants.CmdTaskType.VALIDATION, record.getId(), WxConstants.CmdType.CRK_HELLO_VERIFY_D, WxConstants.CmdPriority.UNKNOWN);

                commandProcessor.addCommand(getCurrentUser(session).getId(),account.getId(),command);

                record.setCmdid(command.getId().intValue());
                record.setCmdstatus(command.getRststatus());
                record.setUpdateTime(new Date());
                wxValidationRecordService.updateRecord(record);
            }

            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

}
